package com.ah.shanxi_demo.controller.web;

import com.ah.shanxi_demo.param.input.PolicyCollectRO;
import com.ah.shanxi_demo.param.input.PolicyPO;
import com.ah.shanxi_demo.param.result.PolicyGeneralRO;
import com.ah.shanxi_demo.param.result.PolicyRO;
import com.ah.shanxi_demo.param.result.SXResult;
import com.ah.shanxi_demo.service.impl.PolicyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.ah.shanxi_demo.convert.ConvertPolicy.convert_policy_ro;
import static com.ah.shanxi_demo.convert.ConvertPolicy.convert_policy_ro_list;

/**
 * @author zt 2020/8/14
 */
@RestController
@Api(value = "陕西高速", description = "陕西高速-策略服务")
@RequestMapping(value = "/api/v1/shanxi/policy/")
public class ApiPolicyController {

    private static final Logger log = LoggerFactory.getLogger(ApiPolicyController.class);

    @Autowired
    private PolicyService policyService;

    @ApiOperation(value = "策略列表查询", notes = "策略列表查询")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public SXResult<List<PolicyRO>> policyList(@RequestBody PolicyPO policyPO) {

        try {
            List<PolicyRO> convertPolicyRoList = convert_policy_ro_list(policyService.listBySelect(policyPO));
            Integer total = policyService.listCountBySelect(policyPO);
            return SXResult.instance(convertPolicyRoList, total);
        } catch (Throwable e) {
            log.error("ApiPolicyController 【list】" + e.getMessage());
        }
        return SXResult.instance();
    }

    @ApiOperation(value = "策略批量删除", notes = "策略批量删除")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public SXResult<Boolean> policyDelete(@RequestBody Long[] policyIdList) {

        try {
            Boolean deleteBoolean = policyService.deletePolicy(policyIdList);
            return SXResult.instance(deleteBoolean);
        } catch (Throwable e) {
            log.error("ApiPolicyController 【delete】" + e.getMessage());
        }
        return SXResult.instance();
    }

    @ApiOperation(value = "策略查看", notes = "策略查看")
    @RequestMapping(value = "show", method = RequestMethod.POST)
    public SXResult<PolicyRO> policyShow(@RequestBody Long policyId) {

        try {
            PolicyRO policyRO = convert_policy_ro(policyService.selectById(policyId));
            return SXResult.instance(policyRO);
        } catch (Throwable e) {
            log.error("ApiPolicyController 【show】" + e.getMessage());
        }
        return SXResult.instance();
    }

    @ApiOperation(value = "策略概览数据", notes = "策略查看")
    @RequestMapping(value = "general", method = RequestMethod.POST)
    public SXResult<PolicyGeneralRO> general() {

        try {
            PolicyGeneralRO general = policyService.general();
            return SXResult.instance(general);
        } catch (Throwable e) {
            log.error("ApiPolicyController 【general】" + e.getMessage());
        }
        return SXResult.instance();
    }


    @ApiOperation(value = "采集数据上传", notes = "采集数据上传")
    @RequestMapping(value = "collect", method = RequestMethod.POST)
    public SXResult<Boolean> policyCollect(@RequestBody PolicyCollectRO policyCollectRO) {
        try {
            Boolean collect = policyService.simulationCollect(policyCollectRO);
            return SXResult.instance(collect);
        } catch (Throwable e) {
            log.error("ApiPolicyController 【general】" + e.getMessage());
        }
        return SXResult.instance();
    }


}
